package com.andyage.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.andyage.admin.bean.MenuBean;
import com.andyage.admin.bean.RoleMenuBean;
import com.andyage.admin.dao.MenuMapper;
import com.andyage.admin.dao.RoleMenuMapper;
import com.andyage.admin.service.IRoleMenuService;
import com.andyage.util.JsonContent;

import net.sf.json.JSONObject;
@Service
public class RoleMenuServiceImpl implements IRoleMenuService {
	@Resource
	private RoleMenuMapper roleMenuMapper;
	@Resource
	private MenuMapper menuMapper;
	@Override
	public String insertRoleMenuInfoBybatch(List<RoleMenuBean> list) throws Exception {
		if(!list.isEmpty()){
			roleMenuMapper.deleteRoleMenuByRoleId(list.get(0));
		}
		int flag=roleMenuMapper.insertRoleMenuInfoBybatch(list);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		return json.toString();
	}

	@Override
	public String queryRoleMenuInfoByroleId(RoleMenuBean roleMenu) throws Exception {
		List<RoleMenuBean> list=roleMenuMapper.queryRoleMenuInfoByroleId(roleMenu);
		
		return JsonContent.getJsonArray(list, 0);
	}

	@Override
	public String deleteRoleMenuByRoleId(RoleMenuBean roleMenu) throws Exception {
		int flag=roleMenuMapper.deleteRoleMenuByRoleId(roleMenu);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		return json.toString();
	}

	@Override
	public List<String> queryStaffRoleByStaffIdAndMenuId(RoleMenuBean roleMenu)throws Exception {
		//查询得到用户所拥有的buttonId
		String buttonId = roleMenuMapper.queryStaffRoleByStaffIdAndMenuId(roleMenu);
		if(buttonId!=null&&!"".equals(buttonId)){
			String[] menuId = buttonId.split(",");//数据库中，按钮的id即为buttonId
			List<Integer> list = new ArrayList<>();
			for (int i = 0; i < menuId.length; i++) {
				list.add(Integer.valueOf(menuId[i]));
			}
			List<String> menuList = menuMapper.queryMenuByMenuId(list);
			return menuList;
		}
		return null;
	}

}
